import pyperclip
import re
import json
'''
手动生成化验结果
'''


def calc(result, range_list: list, info: dict):
    try:
        range_list = [float(x) for x in range_list]
        result = float(result)
        if result > range_list[1]:
            return info.setdefault('升高', '升高')
        elif result < range_list[0]:
            return info.setdefault('降低', '降低')
        else:
            return '正常'
    except ValueError:
        if result in range_list:
            return '正常'
        else:
            return info.setdefault('阳性', '阳性')


def hyd():
    with open('.\\myjson\\hyd.json', 'r', encoding='utf-8') as f:
        hyd = json.load(f)

    hyd_str = pyperclip.paste()
    result = {}
    for k, v in hyd.items():
        for k1, v1 in v.items():
            unit_ = v1[1].replace("^", "\^") if "^" in v1[1] else v1[1]
            find = re.findall(f'{k1}\:([\w.]+)\s{unit_}', hyd_str)

            if find:
                if k not in result.keys():
                    result[k] = {
                        k1: [find[0],
                             calc(find[0], v1[0], v1[2]), v1[1]]
                    }
                else:
                    result[k][k1] = [
                        find[0], calc(find[0], v1[0], v1[2]), v1[1]
                    ]
    result_string = ''
    for kx, vx in result.items():
        result_string += f"{kx}："
        flag = True
        for ky, vy in vx.items():

            if vy[1] == "正常":
                result_string += f"{ky}：{vy[0]} {vy[2]}，"
            else:
                result_string += f"{ky}：{vy[0]} {vy[2]} {vy[1]}，"
                flag = False
        result_string = result_string.strip("，")
        if flag:
            result_string += "，无明显异常；"
        else:
            result_string += "；"
    pyperclip.copy(result_string)
    return result_string


if __name__ == "__main__":
    hyd()